<?php
namespace module\home\model;

use \DataModel;
require_once APP_DIR . '/library/DataModel.php';
require_once APP_DIR . '/library/Auth.php';

/**
 * User Model class
 * 
 * extends from Data Model
 * manages account retrieval and account data manipulation 
 * @package yasns
 * @subpackage model
 */
class User extends DataModel
{
	/**
	 *
	 * @access protected
	 */
	protected
	   $_table = 'account',
	   $_pk = 'account_id';
	

	/**
	 * retrieve user data record matching to specified username and password
	 * only one record should exist
	 * 
	 * @param string $username
	 * @param string $password
	 * @return array user data
	 */
	public static function findByUsernameAndPassword($username, $password)
	{
		$user = self::findByUsername($username);
		if($user && $user->password == $password) return $user;
		else return null;
	}
	
	/**
	 * create new account, and new profile
	 * 
	 * @param string $user
	 * @param string $pass  
	 */
	public function insert()
	{
		// set the default timezone to use
		$timezone = date_default_timezone_set('GMT');
		$format = 'Y-m-d H:i:s';
		$this->created_on = date($format);
		
		// create new Account
		$pk = parent::insert();
		$pk = $this->account_id;

		// create new Profile for newly created Account
		$profile = new Profile();
		$profile->account_id = $pk;
		$profile->insert();
		
		return true;
	}

	/**
	 * find user data by username
	 * 
	 * @param type $username
	 * @return \module\home\model\User 
	 */
	public static function findByUsername($username)
	{
		$conn = DataModel::getConnection();
		$table = "account";
		$fieldUsername = "username";
		
		$stmt = $conn->prepare(	"select * from {$table} " . 
										"where {$fieldUsername} = ?");
										
		$stmt->execute( array($username) );
		$row = $stmt->fetch();
		
		if(!$row) $user = null;
		
		else {
			$user = new User();
			$user->setData($row);
		}
		
		return $user;
		
	}
	
	/**
	 * verify if user is a friend
	 * 
	 * @param int $account_id
	 * @param int $account_friend_id
	 * @return boolean
	 */
	public static function isFriend($account_id, $friend_id)
	{
		$conn = DataModel::getConnection();
		$table = "friend";
		$fieldAccountId = "account_id";
		$fieldAccountFriendId = "account_friend_id";
		
		$stmt = $conn->prepare(	"select account_id from {$table} " . 
										"where {$fieldAccountId} = ? ".
										"AND {$fieldAccountFriendId} = ?");
										
		$stmt->execute( array($account_id, $friend_id) );
		$rows = $stmt->fetchAll();
		
		if (!$rows) { return false; }
		
		return true;
	}
	
	/**
	 * add friend to the database, one way add
	 * 
	 * @param int $account_id
	 * @param int $account_friend_id
	 * @return boolean
	 */
	public function addFriend($account_id, $account_friend_id)
	{
		// create new Friend
		$friend = new Friend();
		$friend->account_id = $account_id;
		$friend->account_friend_id = $account_friend_id;
		$friend->insert();
		
		return true;
	}

	
	/**
	 * delete friend from database based on user id and friend id to be deleted
	 * 
	 * @param int $account_id
	 * @param int $account_friend_id
	 * @return boolean
	 */
	public function deleteFriend($account_id, $account_friend_id)
	{
		$conn = DataModel::getConnection();
		$table = "friend";
		$fieldAccountId = "account_id";
		$fieldFriendId = "account_friend_id";

		$stmt = $conn->prepare(	"delete from {$table} " . 
										"where {$fieldAccountId} = ? AND {$fieldFriendId} = ?");
										
		$stmt->execute( array($account_id, $account_friend_id) );
				
		return true;
	}
	
	/**
	 * find friends of user
	 * 
	 * @param int $account_id
	 * @param int $count 
	 * @return array
	 */
	public static function findFriends($account_id, $count = null)
	{
		$conn = DataModel::getConnection();
		$columns = "friend.account_friend_id, account.username";
		$table = "friend";
		$join = "INNER JOIN account on account.account_id = friend.account_friend_id";
		$field = "friend.account_id";

		// check if count is empty
		$limit = (isset($count)) ? "limit {$count}" : '';
		
		$stmt = $conn->prepare(	"select {$columns} from {$table} {$join} " . 
										"where {$field} = ? {$limit}");
										
		$stmt->execute( array($account_id) );
		$rows = $stmt->fetchAll();
		
		if (!$rows) { return null; }
		
		return $rows;
	}
	
	/**
	 * find friends who added user
	 * 
	 * @param int $account_id
	 * @param int $count 
	 * @return array
	 */
	public static function findFriendsAddedUser($account_id, $count = null)
	{
		$conn = DataModel::getConnection();
		$columns = "friend.account_id, account.username";
		$table = "friend";
		$join = "INNER JOIN account on account.account_id = friend.account_id";
		$field = "account_friend_id";
		  
		// check if count is empty
		$limit = (isset($count)) ? "limit {$count}" : '';
		
		$stmt = $conn->prepare(	"select {$columns} from {$table} {$join} " . 
										"where {$field} = ? {$limit}");
									
		$stmt->execute( array($account_id) );
		$rows = $stmt->fetchAll();
		
		if (!$rows) { return null; }
		
		return $rows;
	}
	
	/**
	 * find recent list of registered users
	 * @param int $count 
	 */
	public static function findByRecent($count = null)
	{	
		$conn = DataModel::getConnection();
		$table = "account";
		
		// check if count is empty
		$limit = (isset($count)) ? "limit {$count}" : '';
		
		$stmt = $conn->prepare(	"select * from {$table} " . 
										"order by created_on DESC {$limit}");
										
		$stmt->execute();
		$rows = $stmt->fetchAll();
		
		if (!$rows) { return null; }
		
		return $rows;
	}
}
?>
